﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using UnityEditor;
using UnityEditor.Scripting.Compilers;

internal class FlashFileHelper
{
    [CompilerGenerated]
    private static Func<string, bool> <>f__am$cache4;
    [CompilerGenerated]
    private static Func<string, IEnumerable<string>> <>f__am$cache5;

    private FlashFileHelper(string stagingArea, string playerPackage, string installPath)
    {
        this.StagingArea = stagingArea;
        this.PlayerPackage = playerPackage;
        this.InstallPath = installPath;
        this.ProjectFolder = Directory.GetCurrentDirectory();
    }

    internal string Combine(string path1, string path2)
    {
        return Path.Combine(path1, path2);
    }

    internal void CopyResourcesFromPlayerPackageToStaging()
    {
        FileUtil.CopyFileOrDirectory(this.Combine(this.PlayerPackage, "Resources"), this.Combine(this.StagingArea, "Resources"));
    }

    public static FlashFileHelper CreateFrom(PostProcessFlashPlayerOptions options)
    {
        return CreateWithBuildArguments(options.StagingAreaData, options.PlayerPackage, options.InstallPath);
    }

    internal static FlashFileHelper CreateWithBuildArguments(string stagingArea, string playerPackage, string installPath)
    {
        return new FlashFileHelper(stagingArea, playerPackage, installPath);
    }

    private IEnumerable<string> FindActionScriptDirectoriesInProject()
    {
        return Directory.GetDirectories(this.Combine(this.ProjectFolder, "Assets"), "ActionScript", SearchOption.AllDirectories);
    }

    internal IEnumerable<string> FindUserSourcePaths()
    {
        return this.FindActionScriptDirectoriesInProject();
    }

    internal IEnumerable<string> FindUserSwcs()
    {
        if (<>f__am$cache5 == null)
        {
            <>f__am$cache5 = dir => Directory.GetFiles(dir, "*.swc", SearchOption.AllDirectories);
        }
        return Enumerable.SelectMany<string, string>(this.FindActionScriptDirectoriesInProject(), <>f__am$cache5);
    }

    internal string FullPathForUnityNativeSwc(string swcFileName)
    {
        return this.PrepareFileName(this.Combine(this.PlayerPackage, swcFileName));
    }

    internal string GetInstallPath()
    {
        return this.PrepareFileName(this.InstallPath);
    }

    internal IEnumerable<string> GetUserAssemblies()
    {
        if (<>f__am$cache4 == null)
        {
            <>f__am$cache4 = s => IsUserCodeAssembly(Path.GetFileName(s));
        }
        return Enumerable.Where<string>(Directory.GetFiles(this.PathForManagedCode(), "*.dll"), <>f__am$cache4).ToArray<string>();
    }

    internal static bool IsUserCodeAssembly(string file)
    {
        if (file.StartsWith("mscorlib.dll"))
        {
            return false;
        }
        if (file.StartsWith("System."))
        {
            return false;
        }
        if (file.StartsWith("Mono."))
        {
            return false;
        }
        if (file.StartsWith("UnityEngine."))
        {
            return false;
        }
        if (file.StartsWith("Boo."))
        {
            return false;
        }
        if (file.StartsWith("UnityScript."))
        {
            return false;
        }
        return true;
    }

    internal string PathForAs3Code()
    {
        return this.StagingArea;
    }

    internal string PathForAs3Src()
    {
        return this.Combine(this.PathForAs3Code(), "src");
    }

    internal string PathForBuildTools()
    {
        return this.Combine(this.PlayerPackage, "BuildTools");
    }

    internal string PathForCil2AsExe()
    {
        return this.PathForFileInCil2As("cil2as.exe");
    }

    internal string PathForConvertedDotNetCode()
    {
        return this.Combine(this.PathForAs3Code(), "ConvertedDotNetCode");
    }

    internal string PathForFileInBuildTools(string file)
    {
        return this.Combine(this.PathForBuildTools(), file);
    }

    public string PathForFileInCil2As(string file)
    {
        return this.Combine(this.Combine(this.PathForBuildTools(), "cil2as"), file);
    }

    public string PathForFileInGendarme(string path)
    {
        return this.Combine(this.Combine(this.PathForBuildTools(), "Gendarme"), path);
    }

    internal string PathForFileInInstallPath(string file)
    {
        return this.Combine(this.PathForInstallationDir(), file);
    }

    internal string PathForFlex(string relativePath)
    {
        return this.PrepareFileName(this.Combine(this.PathForFlexHome(), relativePath).Replace(@"\", "/"));
    }

    internal string PathForFlexFrameworks()
    {
        return this.PathForFlex("frameworks");
    }

    internal string PathForFlexHome()
    {
        return this.Combine(this.PathForBuildTools(), "Flex");
    }

    internal string PathForGendarmeExe()
    {
        return this.PathForFileInGendarme("Gendarme.exe");
    }

    internal string PathForHtmlInInstallPath()
    {
        return this.PathForFileInInstallPath(this.ProjectName() + ".html");
    }

    internal string PathForInstallationDir()
    {
        return Path.GetDirectoryName(this.InstallPath);
    }

    private string PathForManagedCode()
    {
        return this.Combine(this.StagingArea, "Managed");
    }

    internal string PathForMxmlc()
    {
        return this.PathForFlex(this.Combine("lib", "mxmlc.jar"));
    }

    internal string PathForSwfPostProcessorExe()
    {
        return this.Combine(this.PathForBuildTools(), "SwfPostProcessor.exe");
    }

    internal string PathForTempSwf()
    {
        return this.PrepareFileName(this.PathForTempSwfUnprepared());
    }

    internal string PathForTempSwfUnprepared()
    {
        return this.Combine(this.ProjectFolder, this.Combine(this.StagingArea, "temp.swf"));
    }

    internal string PathForUnityAppDotAs()
    {
        return this.PrepareFileName(this.Combine(this.PathForUserBuildAs3(), "UnityApp.as"));
    }

    internal string PathForUnityEngineDLL()
    {
        return this.Combine(this.Combine(this.PlayerPackage, "Managed"), "UnityEngine.dll");
    }

    internal string PathForUserBuildAs3()
    {
        return this.Combine(this.PathForBuildTools(), "UserBuild_AS3");
    }

    internal string PrepareFileName(string file)
    {
        return CommandLineFormatter.PrepareFileName(file);
    }

    internal string ProjectName()
    {
        return Path.GetFileNameWithoutExtension(this.InstallPath);
    }

    internal void SaveUnprocessedSwfCopy()
    {
        FileUtil.CopyFileOrDirectory(this.PathForTempSwf(), this.PathForTempSwf().Replace("temp.swf", "unprocessed.swf"));
    }

    public string InstallPath { get; private set; }

    public string PlayerPackage { get; private set; }

    public string ProjectFolder { get; private set; }

    public string StagingArea { get; private set; }
}

